package com.example.choupaiapp.main.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.ui.fragment.CommonFragment;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.activity.home.CameraActivity;
import com.example.choupaiapp.main.activity.home.VideoPreviewActivity;
import com.example.choupaiapp.main.view.RecordButton;
import com.orhanobut.logger.Logger;
import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraOptions;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.SessionType;
import com.otaliastudios.cameraview.VideoQuality;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ASUS on 2019/12/9.
 */

public class TakeVideoFragment extends CommonFragment {

    @BindView(R.id.rever)
    ImageView rever;
    @BindView(R.id.take_photo)
    RecordButton take_photo;
    Unbinder unbinder;
    public CameraView camera;
    @BindView(R.id.fragment_container)
    FrameLayout fragment_container;
    private String flag;
    @BindView(R.id.long_click)
    TextView long_click;

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            mil = mil + 1;
            if (long_click != null) {
                long_click.setText(mil + "秒/60秒");
            }
            handler.postDelayed(runnable, 1000);
        }
    };

    public static TakeVideoFragment newInstance(String flag) {
        TakeVideoFragment takeVideoFragment = new TakeVideoFragment();
        takeVideoFragment.flag = flag;
        return takeVideoFragment;
    }


    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_take_video, container, false);
        unbinder = ButterKnife.bind(this, view);

        EventBus.getDefault().register(this);


        return view;
    }

    private void onVideo(File video) {
        Logger.e(video.getAbsolutePath());
        long_click.setVisibility(View.INVISIBLE);
        Intent intent = new Intent(context, VideoPreviewActivity.class);
        intent.putExtra("video", video.getAbsolutePath());
        intent.putExtra("flag", flag);
//        intent.putExtra("video", Uri.fromFile(video));
        startActivity(intent);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (camera != null) {
            camera.start();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (camera != null) {
            camera.stop();
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (camera != null) {
            camera.destroy();
        }
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    /**
     * 将播放器从父控件中移除
     */
    private void removePlayerFormParent(View View) {
        if (View != null) {
            ViewParent parent = View.getParent();
            if (parent instanceof FrameLayout) {
                ((FrameLayout) parent).removeView(View);
            }
        }
    }

    private int mil = 0;

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void OnMessageEvent(String event) {
        if (event.equals("takevideo")) {
            View view = CameraActivity.view;
            camera = view.findViewById(R.id.camera);
            camera.setSessionType(SessionType.VIDEO);
            camera.setVideoQuality(VideoQuality.HIGHEST);
            camera.setLifecycleOwner(this);
            camera.addCameraListener(new CameraListener() {
                public void onCameraOpened(CameraOptions options) {
                }

                public void onPictureTaken(byte[] jpeg) {
                }

                @Override
                public void onVideoTaken(File video) {
                    super.onVideoTaken(video);
                    if (mil >= 3) {
                        onVideo(video);
                    } else {
                        showImageToas("拍摄时间不能小于3秒");
                    }
                }
            });


            take_photo.setTimeLimit(120000);
            take_photo.setOnRecordButtonListener(new RecordButton.OnRecordButtonListener() {
                @Override
                public void onClick() {

                }

                @Override
                public void onLongClickStart() {
                    camera.startCapturingVideo(null, 120000);
//                    exitTime = System.currentTimeMillis();
                    long_click.setVisibility(View.VISIBLE);
                    mil = -1;
                    long_click.setText(0 + "秒/120秒");
                    handler.postDelayed(runnable, 1000);
                }

                @Override
                public void onLongClickEnd() {
                    handler.removeCallbacks(runnable);
                    camera.stopCapturingVideo();
                }
            });


            rever.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isDoubleClick()) {
                        return;
                    }
                    camera.toggleFacing();
                }
            });

            removePlayerFormParent(view);
            fragment_container.addView(view);

            camera.start();

            long_click.setVisibility(View.VISIBLE);

        }

    }


}
